package com.mrd.gtimp.base.action;

import com.mrd.constant.MsgConstants;
import com.mrd.constant.WebPathConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.config.web.json.annotation.JField;
import com.mrd.framework.config.web.json.annotation.Json;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.framework.utils.HttpUtils;
import com.mrd.gtimp.base.entity.Company;
import com.mrd.gtimp.base.entity.Dept;
import com.mrd.gtimp.base.entity.Role;
import com.mrd.gtimp.base.entity.User;
import com.mrd.gtimp.base.service.CompanyBiz;
import com.mrd.gtimp.base.service.DeptBiz;
import com.mrd.gtimp.base.service.RoleBiz;
import com.mrd.gtimp.base.service.UserBiz;
import com.mrd.gtimp.business.service.ContacterBiz;
import com.mrd.util.LoginUtil;
import com.mrd.util.endecrypt.MD5Util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author qiul
 */

@Controller
@RequestMapping("/base")
public class CompanyAction {
    private static final long serialVersionUID = 1L;

    @Resource
    private CompanyBiz companyBiz;

    @Resource
    private DeptBiz deptBiz;

    @Resource
    private UserBiz userBiz;

    @Resource
    private RoleBiz roleBiz;

    @Resource
    private ContacterBiz contacterBiz;

    /**
     * public String show() {
     * try {
     * page = initPage(CompanyBiz.findEntityCount(filters));
     * Order order = new Order();
     * order.add(sidx, sord);
     * Companys = CompanyBiz
     * .findEntityByFilter(page.getCurrentPageStartNo(), page.getPageSize(), filters, order);
     * } catch (Exception e) {
     * e.printStackTrace();
     * }
     * return SUCCESS;
     * }
     **/

    /**
     * 总公司管理页面
     */
    @RequestMapping("/company-main")
    public String main(Model model) {
//        title = "公司管理";
//        left = "company-mainCompany.do";
//        right = "";
        model.addAttribute("title", "公司管理");
        model.addAttribute("left", "company-mainCompany");
        model.addAttribute("right", "");
        return WebPathConstants.FRAME_WEB_PATH;
    }

    /**
     * 跳转公司页面
     */
    @RequestMapping("/company-mainCompany")
    public String mainCompany() {
        return "/gtimp/base/companyList";
    }

    /**
     * 分页展示公司列表
     */
    @RequestMapping("/company-listCompany")
    @ResponseBody
    @Json({@JField("id"), @JField("name"), @JField("isParent"), @JField("mobile"), @JField("isabled"), @JField("validate"), @JField("state"), @JField("port"), @JField("port_code")})
    public List<Company> listCompany(Jqgrid jqgrid) {
        try {
            jqgrid.addRule(new SearchRule("isabled", SearchRuleOption.eq, "0"));
            jqgrid.addRule(new SearchRule("state", SearchRuleOption.eq, "0"));
            Page<Company> pageResult = companyBiz.findByJqgrid(jqgrid);
            for (Company com : pageResult) {
                com.setIsParent(true);
            }
            return pageResult.getContent();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 新增或修改公司信息
     */
    @RequestMapping("/company-saveOrUpdateCompany")
    @ResponseBody
    public Result saveOrUpdateCompany(Company company) {
        Result msg;
        try {
            if (StringUtils.isEmpty(company.getId())) {  // 新增公司操作
                company.setValidate("0");
                company.setIsabled("0");
                company.setState("0");
                company.setCreator("");
                company.setCtime(new Date());
                company.setId(null);
            } else {  // 修改公司操作
                Company temp = companyBiz.findEntityById(company.getId());
                if (temp == null) {
                    throw new Exception("找不到公司，id=" + company.getId());
                }
                temp.setName(company.getName());
                temp.setType(company.getType());
                temp.setPhone(company.getPhone());
                temp.setFax(company.getFax());
                temp.setContactor(company.getContactor());
                temp.setMobile(company.getMobile());
                temp.setAddress(company.getAddress());
                temp.setRemarks(company.getRemarks());
                temp.setModifier(LoginUtil.getLoginAccount());
                temp.setMtime(new Date());
                temp.setIsSignWS(company.getIsSignWS());
                company = temp;
            }
            companyBiz.saveOrupdateEntity(company);
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL);
        }
        return msg;
    }

    /**
     * 编辑公司信息
     */
    @RequestMapping("/company-manageCompany")
    public String manageCompany(Company company, Model model) {
        if (company != null && StringUtils.isNotEmpty(company.getId())) {
            try {
                company = companyBiz.findEntityById(company.getId());
                model.addAttribute("company", company);
            } catch (QueryException e) {
                e.printStackTrace();
            }
        }
        return "/gtimp/base/companyManage";
    }

    /**
     * 删除公司
     */
    @RequestMapping("/company-delCompany")
    public String delCompany(Company company) {
        company.setState("1");
        return "";
    }

    /**
     * 跳转部门页面
     */
    @RequestMapping("/company-mainDept")
    public String mainDept(Company company, Model model, String companyId) {
        if (null != company) {
            if (StringUtils.isNotEmpty(company.getId())) {
                companyId = company.getId();
            }
        }
        model.addAttribute("companyId", companyId);
        return "/gtimp/base/deptList";
    }

    /**
     * 客户、供应商公司管理部门跳转页面
     */
    @RequestMapping("/company-deptList")
    public String deptList(String companyId, Model model) {
        companyId = LoginUtil.getLoginUser().getCompany().getId();
        model.addAttribute("companyId", companyId);
        return "/gtimp/base/deptList";
    }

    /**
     * 添加或修改部门信息
     */
    @RequestMapping("/company-saveOrUpdateDept")
    @ResponseBody
    public Result saveOrUpdateDept(Dept dept, String companyId) {
        Result msg;
        try {
            if (StringUtils.isNotEmpty(companyId)) {
                Company company = companyBiz.findEntityById(companyId);
                if (company != null) {
                    if (StringUtils.isEmpty(dept.getId())) {
                        dept.setId(null);
                        dept.setState("0");
                        dept.setIsabled("0");
                        dept.setCreator(LoginUtil.getLoginUser().getAccount());
                        dept.setCtime(new Date());
                        dept.setCompany(company);
                        deptBiz.saveOrupdateEntity(dept);
                    } else {
                        dept.setModifier(LoginUtil.getLoginUser().getAccount());
                        dept.setMtime(new Date());
                        List<String> properties = new ArrayList<>();
                        properties.add("name");
                        properties.add("modifier");
                        properties.add("mtime");
                        deptBiz.updateEntity(dept, properties);
                    }
                }
            }
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL);
        }
        return msg;
    }

    /**
     * 根据条件分页列出部门列表
     */
    @RequestMapping("/company-listDept")
    @ResponseBody
    public Map<String, Object> listDept(Company company, Jqgrid jqgrid) {
        if (company != null && StringUtils.isNotEmpty(company.getId())) {
            jqgrid.addRule(new SearchRule("company.id", SearchRuleOption.eq, company.getId()));
            //jqgrid.addRule(new SearchRule("state", SearchRuleOption.eq, "0"));
        }
        try {
            Map<String, Object> result = new HashMap<>();
            Page<Dept> depts = deptBiz.findByJqgrid(jqgrid);
            if (depts != null && depts.getSize() > 0) {
                List<Map<String, Object>> list = new ArrayList<>();
                for (Dept d : depts) {
                    d.setIsParent(false);
                    Map<String, Object> tempMap = new HashMap<>();
                    tempMap.put("id", d.getId());
                    tempMap.put("isParent", d.getIsParent());
                    tempMap.put("isabled", d.getIsabled());
                    tempMap.put("name", d.getName());
                    tempMap.put("state", d.getState());
                    list.add(tempMap);
                }
                result.put("depts", list);
            }
            return result;
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 展示部门信息或者空白信息
     */
    @RequestMapping("/company-manageDept")
    public String manageDept(String companyId, String deptId, Model model) {
        if (StringUtils.isNotEmpty(deptId)) {
            try {
                Dept dept = deptBiz.findEntityById(deptId);
                model.addAttribute("dept", dept);
            } catch (QueryException e) {
                e.printStackTrace();
            }
        }
        model.addAttribute("companyId", companyId);
        return "/gtimp/base/deptManage";
    }

    /**
     * 删除部门
     */
    @RequestMapping("/company-delDept")
    @ResponseBody
    public Result delDept(Dept dept, String deptId) {
        Result msg;
        try {
            dept = deptBiz.findEntityById(deptId);
            List<String> properties = new ArrayList<String>();
            properties.add("state");
            dept.setState("1");
            deptBiz.updateEntity(dept, properties);
            String sql = "update t_sys_user u set u.u_state = '1' where u.u_id in (select t.u_id from t_sys_dept_user t where t.dept_id = '"
                    + deptId + "')";
            userBiz.updateBySql(sql);
            msg = Result.newSuccessInstance(MsgConstants.OPERATION_SUCCESS);
        } catch (Exception e) {
            msg = Result.newFailInstance(MsgConstants.OPERATION_FAIL);
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 检测是否为管理部门
     */
    @RequestMapping("/company-checkIsManageDept")
    @ResponseBody
    public Result checkIsManageDept(String deptId) {
        Result msg = null;
        String sql = "select u.role_id from t_sys_dept_user t, t_sys_role_user u "
                + "where u.user_id = t.u_id and t.dept_id = '" + deptId + "'";
        try {
            List<Object> list = userBiz.findObjectBySql(sql, 0, 10, null);
            if (list.size() > 0) {
                if (list.get(0).equals("4028803e5262fa340152632f9b2d0004")) {// 客户公司管理员
                    msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES, "{\"state\":\"companyManage\"}");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 跳转用户页面
     */
    @RequestMapping("/company-mainUser")
    public String mainUser(Dept dept, String deptId, Model model) {
//		deptId = findDeptIdByUserId();
//		String state = HttpUtils.getParam("state");
//		request.setAttribute("state", state);
        if (null != dept) {
            if (StringUtils.isNotEmpty(dept.getId())) {
                deptId = dept.getId();
            }
        }
        model.addAttribute("deptId", deptId);
        return "/gtimp/base/userList";
    }


    /**
     * 公司管理用户界面
     */
    @RequestMapping("/company-companyMainUser")
    public String companyMainUser(String deptId, Model model) {
        deptId = findDeptIdByUserId();
        model.addAttribute("deptId", deptId);
        return "/gtimp/base/userList";
    }


    /**
     * 根据用户名查部门id
     */
    @RequestMapping("/company-findDeptIdByUserId")
    public String findDeptIdByUserId() {
        String result = "";
        String sql = "select t.dept_id from t_sys_dept_user t where t.u_id = '" + LoginUtil.getLoginUser().getId() + "'";
        try {
            List<Object> list = userBiz.findObjectBySql(sql, 0, 10, null);
            if (list.size() > 0) {
                result = (String) list.get(0);
                return result;
            }
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return result;

    }

    /**
     * 根据条件分页列出用户列表
     */
    @RequestMapping("/company-listUser")
    @ResponseBody
    public Map<String, Object> listUser(@RequestParam(required = false) String deptId, Jqgrid jqgrid) {
        try {
            if (StringUtils.isNotEmpty(deptId)) {
                jqgrid.addRule(new SearchRule("id", SearchRuleOption.eq, deptId));
                Page<Dept> deptPage = deptBiz.findByJqgrid(jqgrid);
                Map<String, Object> result = null;
                if (deptPage != null) {
                    List<Dept> deptList = deptPage.getContent();
                    for (Dept dept : deptList) {
                        List<User> userList = dept.getUsers();
                        if (userList != null && userList.size() > 0) {
                            result = new HashMap<>();
                            List<Map<String, Object>> tempUserList = new ArrayList<>();
                            for (User user : userList) {
                                Map<String, Object> tempMap = new HashMap<>();
                                tempMap.put("id", user.getId());
                                tempMap.put("isabled", user.getIsabled());
                                tempMap.put("mobile", user.getMobile());
                                tempMap.put("name", user.getName());
                                tempMap.put("state", user.getState());
                                tempMap.put("type", user.getType());
                                tempUserList.add(tempMap);
                            }
                            Map<String, Object> pageMap = new HashMap<>();
                            pageMap.put("currentPage", jqgrid.getPage().getPageNumber() + 1);
                            pageMap.put("currentPageLastNo", "");
                            pageMap.put("currentPageStartNo", "");
                            pageMap.put("hasNextPage", "");
                            pageMap.put("pageSize", jqgrid.getPage().getPageSize());
                            pageMap.put("totle", userList.size());
                            pageMap.put("totlePage", userList.size() / jqgrid.getPage().getPageSize());
                            result.put("page", pageMap);
                            result.put("users", tempUserList);
                        }
                    }
                }
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 展示用户信息或者空白信息
     */
    @RequestMapping("/company-manageUser")
    public String manageUser(User user, String companyType, String deptId, Model model) {
        try {
            if (StringUtils.isNotEmpty(user.getId())) {
                user = userBiz.findEntityById(user.getId());
                //// TODO: 2016/8/28  查询下拉框数据
                // companyType = user.getCompany().getType();
                //  request.setAttribute("companyType", companyType);
                model.addAttribute("users", user);
            }
            model.addAttribute("deptId", deptId);
        } catch (QueryException e) {
            e.printStackTrace();
        }
        return "/gtimp/base/userManage";
    }

    /**
     * 添加或修改用户信息
     */
    @RequestMapping("/company-saveOrUpdateUser")
    @ResponseBody
    public Result saveOrUpdateUser(User user, String deptId) {
        Result msg;
        try {
            if (null != user) {
                if (StringUtils.isEmpty(user.getId())) {  // 新增人员
                    user.setId(null);
                    user.setIsabled("0");
                    user.setState("0");
                    user.setCreator(LoginUtil.getLoginUser().getAccount());
                    user.setCtime(new Date());
                    user.setPwd(MD5Util.string2MD5(user.getPwd()));
                    List<Dept> depts = new ArrayList<Dept>();
                    Dept dept = deptBiz.findEntityById(deptId);
                    depts.add(dept);
                    user.setDepts(depts);
//                    String companyType = LoginUtil.getLoginUser().getCompany().getType();
                    String companyType = dept.getCompany().getType();
                    List<Role> roles = new ArrayList<Role>();
                    Role role = null;
                    if (companyType.equals("CL")) {
                        if (user.getType().equals("0")) {
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24ffd8900d9"));// 客户公司管理
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24df35700ce"));// 客户普通权限
                        } else if (user.getType().equals("1")) {
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24ffd8900d9"));// 客户公司管理
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24df35700ce"));// 客户普通权限
                        } else if (user.getType().equals("2")) {
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24df35700ce"));// 客户普通权限
                        }
                    } else if (companyType.equals("CC")) {
                        if (user.getType().equals("0")) {
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24da7aa00cd"));// 供应商公司管理
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24e4f5f00cf"));// 供应商普通权限
                        } else if (user.getType().equals("1")) {
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24da7aa00cd"));// 供应商公司管理
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24e4f5f00cf"));// 供应商普通权限
                        } else if (user.getType().equals("2")) {
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24da7aa00cd"));// 供应商普通权限
                        }
                    } else if (companyType.equals("CM")) {
                        roles.add(roleBiz.findEntityById("402880bd3c09d29e013c09d2c6d80000"));// 运营管理员
                        roles.add(roleBiz.findEntityById("402880bd3c09d29e013c09d338400003"));// 运营管理员
                        roles.add(roleBiz.findEntityById("f0fc94325986054301598625e507006d"));// 运营管理员
                    }
                    roles.add(role);
                    user.setRoles(roles);
                    userBiz.saveOrupdateEntity(user);
                } else {// 修改人员
                    User userTmp = userBiz.findEntityById(user.getId());

                    userTmp.setName(user.getName());
                    if (StringUtils.isNotEmpty(user.getPwd())) {
                        userTmp.setPwd(MD5Util.string2MD5(user.getPwd()));
                    }
                    userTmp.setMobile(user.getMobile());
                    userTmp.setAccount(user.getAccount());
                    userTmp.setEmail(user.getEmail());
                    userTmp.setType(user.getType());
                    userTmp.setTelphone(user.getTelphone());

                    List<Role> roles = new ArrayList<Role>();
                    String companyType = userTmp.getCompany().getType();
                    if (companyType.equals("CL")) {
                        if (user.getType().equals("0")) {
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24ffd8900d9"));// 客户公司管理
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24df35700ce"));// 客户普通权限
                        } else if (user.getType().equals("1")) {
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24ffd8900d9"));// 客户公司管理
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24df35700ce"));// 客户普通权限
                        } else if (user.getType().equals("2")) {
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24df35700ce"));// 客户普通权限
                        }
                    } else if (companyType.equals("CC")) {
                        if (user.getType().equals("0")) {
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24da7aa00cd"));// 供应商公司管理
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24e4f5f00cf"));// 供应商普通权限
                        } else if (user.getType().equals("1")) {
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24da7aa00cd"));// 供应商公司管理
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24e4f5f00cf"));// 供应商普通权限
                        } else if (user.getType().equals("2")) {
                            roles.add(roleBiz.findEntityById("f0fc943258f1574c0158f24da7aa00cd"));// 供应商普通权限
                        }
                    } else if (companyType.equals("CM")) {
                        roles.add(roleBiz.findEntityById("402880bd3c09d29e013c09d2c6d80000"));// 运营管理员
                        roles.add(roleBiz.findEntityById("402880bd3c09d29e013c09d338400003"));// 运营管理员
                        roles.add(roleBiz.findEntityById("f0fc94325986054301598625e507006d"));// 运营管理员
                    }
                    userTmp.setRoles(roles);
                    userTmp.setModifier(LoginUtil.getLoginUser().getAccount());
                    userTmp.setMtime(new Date());
                    // 在这里用saveOupdatdeEntity()这个方法出错，它会在最后执行删除表中数据的操作 原因未知 待查
                    // userBiz.saveOrupdateEntity(user);
//                    userBiz.updateEntity(user, propertyList);
                    userBiz.saveOrupdateEntity(userTmp);
                }
            }
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL);
        }
        return msg;
    }

    /**
     * 删除用户
     */
    @RequestMapping("/company-delUser")
    @ResponseBody
    public Result delUser() {
        Result msg;
        String userId = HttpUtils.getParam("userId");
        try {
            User user = userBiz.findEntityById(userId);
            List<String> properties = new ArrayList<String>();
            properties.add("state");
            user.setState("1");
            userBiz.updateEntity(user, properties);
            msg = Result.newSuccessInstance(MsgConstants.DEL_SUCCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.DEL_FAIL);
        }
        return msg;
    }

    /**
     * 展示账号详细信息
     */
    @RequestMapping("/company-bizShowUser")
    public String bizShowUser(User user, Model model) {
        if (null == user || StringUtils.isEmpty(user.getId())) {
            try {
                user = userBiz.findEntityById(LoginUtil.getLoginUser().getId());
            } catch (QueryException e1) {
                e1.printStackTrace();
            }
            model.addAttribute("user", user);
        }
        return "/gtimp/base/userBizView";
    }

    /**
     * 密码重置
     */
    @RequestMapping("/company-resetPwd")
    @ResponseBody
    public Result resetPwd(User user) {
        Result msg = null;
        try {
            String cpwd = HttpUtils.getParam("cpwd");
            String newpwd = HttpUtils.getParam("newpwd");
            user = userBiz.findEntityById(LoginUtil.getLoginUser().getId());
            if (user.getPwd().equals(MD5Util.string2MD5(cpwd))) {
                user.setPwd(MD5Util.string2MD5(newpwd));
                user.setModifier(LoginUtil.getLoginUser().getAccount());
                user.setMtime(new Date());
                List<String> properties = new ArrayList<String>();
                properties.add("pwd");
                properties.add("modifier");
                properties.add("mtime");
                userBiz.updateEntity(user, properties);
                msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES, "请使用新密码重新登录！");
            } else {
                msg = Result.newFailInstance(MsgConstants.OPERATION_FAIL, "原密码输入错误！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 检查用户是否存在
     */
    @RequestMapping("/company-checkUserExist")
    @ResponseBody
    public Result checkUserExist() {
        Result msg = null;
        List<User> users = new ArrayList<>();
        String account = HttpUtils.getParam("account");
        // String sql = "select u.* from t_sys_user u where u.u_account = '" + account + "'";
        String hql = "from com.mrd.gtimp.base.entity.User user where user.account='" + account + "'";
        try {
            users = userBiz.findEntityByHql(hql, 0, 10, null);
            if (users.size() > 0) {
                if ("1".equals(users.get(0).getState())) {
                    msg = Result.newFailInstance("{\"state\":\"1\",\"msg\":\"该账号已被禁用删除，请换其他用户账号注册!\"}");
                } else if ("0".equals(users.get(0).getState())) {
                    msg = Result.newFailInstance("{\"state\":\"1\",\"msg\":\"该账号已存在，请换其他用户账号注册!\"}");
                }
            } else {
                msg = Result.newSuccessInstance("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 检查部门是否存在
     */
    @RequestMapping("/company-checkDeptExist")
    @ResponseBody
    public Result checkDeptExist(Jqgrid jqgrid) {
        Result msg = null;
        String dept_name = HttpUtils.getParam("dept_name");
        try {
            jqgrid.addRule(new SearchRule("name", SearchRuleOption.eq, dept_name));
            jqgrid.addRule(new SearchRule("company.id", SearchRuleOption.eq, LoginUtil.getLoginUser().getCompany().getId()));
            Page<Dept> deptPage = deptBiz.findByJqgrid(jqgrid);
            List<Dept> list = deptPage.getContent();
            if (list != null && !list.isEmpty()) {
                for (Dept dept : list) {
                    if (dept.getState().equals("1")) {
                        msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES, "{\"state\":\"1\",\"msg\":\"该部门名称已被禁用删除，请换其他名称注册!\"}");
                        return msg;
                    } else if (dept.getState().equals("0")) {
                        msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES, "{\"state\":\"1\",\"msg\":\"该部门名称已存在，请换其他名称注册!\"}");
                        return msg;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }


    @RequestMapping("/company-findEntityById")
    @ResponseBody
    @Json({@JField("com_customs_code"), @JField("name"), @JField("contactor"), @JField("phone"), @JField("mobile")})
    public Company findEntityById(String companyId) {
        if (StringUtils.isNotEmpty(companyId)) {
            try {
                return companyBiz.findEntityById(companyId);
            } catch (QueryException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
